home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / CopyBits & CopyMask / CopyBits meets CopyMask.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  4KB  |  146 lines

  1. #include <QDOffScreen.h>
  2.  
  3. /* Standard inits */
  4.  
  5.  
  6. #define    kLoopNumber    100
  7.  
  8.  
  9.  
  10. static void InitToolbox(void)
  11. {
  12.     InitGraf (&qd.thePort);
  13.     InitFonts ();
  14.     FlushEvents (everyEvent,0);
  15.     InitWindows ();
  16.     InitMenus ();
  17.     TEInit ();
  18.     InitDialogs (nil);
  19.     InitCursor ();
  20. } /*InitToolbox*/
  21.  
  22.  
  23. void main(void)
  24. {
  25.     WindowPtr    myWindow;
  26.     Rect    windowRectangle;
  27.     short    h,v;
  28.     GrafPtr    screenPort;
  29.     GDHandle    screenDevice;
  30.     Rect    pictRectangle, srcRectangle;
  31.     PicHandle    coolPicture, maskPicture;
  32.     CTabHandle    cTable;
  33.     GrafPtr    offscreenGWorld = nil, maskGWorld = nil;
  34.     RgnHandle    maskRegion;
  35.  
  36.     
  37. // offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to
  38. // refer to them by GrafPtrs, since the CopyBits calls get much cleaner.
  39.  
  40. /* Variables for loops, timing and display */
  41.     long    beforeTime, afterTime;
  42.     long    loop;
  43.     Str255    tempStr;
  44.  
  45.  
  46.     InitToolbox();
  47.  
  48. /* Load the picture*/
  49.     coolPicture = GetPicture(128);            /*PICT resource #128.*/
  50.     if (coolPicture == nil)
  51.     {
  52.         SysBeep(1);
  53.         ExitToShell();
  54.     }
  55.  
  56.     maskPicture = GetPicture(129);            /*PICT resource #129.*/
  57.     if (maskPicture == nil)
  58.     {
  59.         SysBeep(1);
  60.         ExitToShell();
  61.     }
  62.  
  63. /* Get its frame */
  64.     pictRectangle = (**coolPicture).picFrame;
  65. /* Move it to 0,0 */
  66.     OffsetRect(&pictRectangle, -pictRectangle.left,  -pictRectangle.top);
  67.  
  68. /*Set up the window*/
  69.     windowRectangle = pictRectangle;
  70.     OffsetRect(&windowRectangle, 64, 64);
  71.     windowRectangle.bottom += 100;
  72.     myWindow = NewCWindow(0L, &windowRectangle, "\pCopyBitsSpeedTest", true, 0, (WindowPtr)-1L, false, 0);
  73.     SetPort(myWindow);
  74.  
  75. //    DrawPicture(coolPicture, &pictRectangle);
  76.  
  77. /* Remember it so we can get back after visiting a GWorld */
  78.     GetGWorld((GWorldPtr *)&screenPort, &screenDevice);
  79.  
  80. /* Create a GWorld to draw the PICT in */
  81.     if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 0, &pictRectangle, nil, nil, 0) )
  82.         ExitToShell();
  83. /*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
  84.     if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
  85.         ;
  86.     SetGWorld((GWorldPtr)offscreenGWorld, nil);
  87.     
  88.     PaintRect(&pictRectangle);
  89.     DrawPicture(coolPicture, &pictRectangle);
  90.  
  91. /* Create a GWorld to draw the mask in */
  92.     if ( noErr != NewGWorld((GWorldPtr *)&maskGWorld, 1, &pictRectangle, nil, nil, 0) )
  93.         ExitToShell();
  94. /*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
  95.     if ( LockPixels(((CGrafPtr)maskGWorld)->portPixMap) )
  96.         ;
  97.     SetGWorld((GWorldPtr)maskGWorld, nil);
  98.     
  99.     EraseRect(&pictRectangle);
  100.     DrawPicture(maskPicture, &pictRectangle);
  101.  
  102.     maskRegion = NewRgn();
  103.     BitMapToRegion(maskRegion, &(maskGWorld->portBits));
  104.  
  105. /* GWorlds done. Set back port and device! */
  106.  
  107.     SetGWorld((GWorldPtr)screenPort, screenDevice);
  108.  
  109. //    CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
  110.  
  111. // CopyBits
  112.  
  113.     srcRectangle = pictRectangle;
  114.  
  115.     beforeTime = TickCount();
  116.     for (loop = 0; loop < kLoopNumber; loop++)
  117.     {
  118.         srcRectangle = pictRectangle;
  119.         OffsetRect(&srcRectangle, 0, loop % 20 - 10);
  120.         CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &srcRectangle, &pictRectangle, srcCopy, maskRegion);
  121.     }
  122.     afterTime = TickCount();
  123.     MoveTo(10, windowRectangle.bottom - 80 - 64);
  124.     NumToString(afterTime - beforeTime, tempStr);
  125.     DrawString("\pCopyBits: ");
  126.     DrawString(tempStr);
  127.     DrawString("\p ticks.");
  128.  
  129.     beforeTime = TickCount();
  130.     for (loop = 0; loop < kLoopNumber; loop++)
  131.     {
  132.         srcRectangle = pictRectangle;
  133.         OffsetRect(&srcRectangle, 0, loop % 20 - 10);
  134.         CopyMask(&offscreenGWorld->portBits, &maskGWorld->portBits, &myWindow->portBits, &srcRectangle, &pictRectangle, &pictRectangle);
  135.     }
  136.     afterTime = TickCount();
  137.     MoveTo(10, windowRectangle.bottom - 50 - 64);
  138.     NumToString(afterTime - beforeTime, tempStr);
  139.     DrawString("\pCopyMask: ");
  140.     DrawString(tempStr);
  141.     DrawString("\p ticks.");
  142.  
  143.  
  144.     while (!Button());
  145. } /*main*/
  146.